$(function () {

	// 字典类型表格
	$("#typeGrid").jqGrid({
		url: baseURL + 'sys/dict/page',
		datatype: "json",
		colModel: [
			{ label: '类型名称', name: 'name', index: 'name', width: 90 },
			{ label: '类型', name: 'type', index: 'type', width: 90 },
			{ label: '排序', name: 'orderNum', index: 'order_num', width: 30 },
			{ label: '备注', name: 'remark', index: 'remark', width: 80 },
			{label: '操作', name: '', index: 'operate', width: 100, align: 'center',
				formatter: function (value, options, row) {
					/*var detail='<button type="button" class="btn btn-danger btn-sm">' +
						'  <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>' +
						'</button>';
					return detail;*/
					return '<input class="btn btn-primary btn-xs" type="button" value="修改" onclick="vm.updateRowType(\''+row.id+'\')">'
					      + "&nbsp;"
						  + '<input class="btn btn-danger btn-xs" type="button" value="删除" onclick="vm.delRowType(\''+row.id+'\')">'
						  + "&nbsp;"
						  + '<input class="btn btn-info btn-xs" type="button" value="详情" onclick="vm.showItem(\''+row.id+'\',\''+row.type+'\')">';
				},
			}
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#typeGridPager",
		jsonReader : {
			root: "page.records",
			page: "page.page",
			total: "page.pages",
			records: "page.total"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		postData: {
			parentId: "0" // 默认查询父节点为0的字典
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});

	// 字典详情表格
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/dict/page',
        datatype: "json",
        colModel: [
			{ label: '名称', name: 'name', index: 'name', width: 80 },
			{ label: '字典码', name: 'code', index: 'code', width: 80 },
			{ label: '字典值', name: 'value', index: 'value', width: 80 },
			{ label: '排序', name: 'orderNum', index: 'order_num', width: 80 },
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }
		],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
		jsonReader : {
			root: "page.records",
			page: "page.page",
			total: "page.pages",
			records: "page.total"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		postData: {
        	parentId: "-1" // 默认查询父节点为0的字典
		},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
        itemParam:{
            name: null
        },
		typeParam:{
        	name: null
		},
		showList: true,
		title: null,
		dict: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		queryType: function () {
			vm.reloadType();
		},
		add: function(){
			vm.dict = {};
			if (vm.itemParam.parentId) {
				vm.dict.parentId = vm.itemParam.parentId
				vm.dict.type = vm.itemParam.type
			}else {
				alert("请先选择字典类型");
				return;
			}
			vm.showList = false;
			vm.title = "新增";
		},
		addType: function(){
			vm.dict = {
				parentId: 0
			};
			vm.showList = false;
			vm.title = "新增类型";
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改字典项";
            vm.getInfo(id)
		},
		updateType: function () {
			var id = getSelectedRow("typeGrid");
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改类型";
            vm.getInfo(id)
		},
		updateRowType: function(id) {
			vm.showList = false;
			vm.title = "修改类型";
			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dict.id == null ? "sys/dict/save" : "sys/dict/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dict),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							if (vm.dict.parentId == 0) {
								vm.dict = {}
								vm.reloadType();
							}else {
								vm.reload();
							}
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的字典项？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/dict/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		delType: function (event) {
			var ids = getSelectedRows("typeGrid");
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的字典类型？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/dict/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#typeGrid").trigger("reloadGrid");
								if (ids.indexOf(vm.itemParam.parentId) > -1) {
									vm.itemParam.parentId = -1
								}
								if (vm.dict.parentId && ids.indexOf(vm.dict.parentId)) {
									$("#jqpeGrid").trigger("reloadGrid");
								}
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		delRowType: function(id) {
			const ids = []
			ids[0] = id
			confirm('确定要删除选中的字典类型？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "sys/dict/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#typeGrid").trigger("reloadGrid");
								if (ids.indexOf(vm.itemParam.parentId) > -1) {
									vm.itemParam.parentId = -1
								}
								if (vm.dict.parentId && ids.indexOf(vm.dict.parentId)) {
									$("#jqpeGrid").trigger("reloadGrid");
								}
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/dict/info/"+id, function(r){
                vm.dict = r.dict;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			console.info(page)
			console.info(vm.itemParam)
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                	name: vm.itemParam.name,
					parentId: vm.itemParam.parentId
				},
                page:page
            }).trigger("reloadGrid");
		},
		reloadType: function (event) {
			vm.showList = true;
			var page = $("#typeGrid").jqGrid('getGridParam','page');
			$("#typeGrid").jqGrid('setGridParam',{
				postData:{
					name: vm.typeParam.name,
					parentId: 0
				},
				page:page
			}).trigger("reloadGrid");
			vm.itemParam.parentId = -1
			vm.reload()
		},
		showItem:function (id,type) {
			vm.itemParam.parentId = id;
			vm.itemParam.type = type
			vm.reload();
		}
	}
});